import { EText, EPad, EPin, ELine, EArc, EShape} from "./DrawEncaClass";

// 画图策略
const insertStrategy = {
    PIN(option){
        let pad = {};

        let pin = {};

        let pinList = [];

        let padList = [];

        if(options.form == "SELF"){

            pad = option.pad;

        }else{
            pad = EPad.createPad(option.padOption);
        }

        pin = createPin(pinInfo,{x:0,y:0});

        pinList.push(pin);

        padList.push(pad);

        // 创建一个pin
        function createPin(pinConfig,point){
            let cpin = {
                padid:pinConfig.padid,
                pinid: pinConfig.pinid,
                number: pinConfig.pin_num,
                x:point.x,
                y:point.y
            };
            return new EPin(cpin);
        };

        // 处理需要编辑的数据
        function returnEditData(pin,config){
            var result = {
                pin_num:pin.pin_num,
                x:pin.x,
                y:pin.y,
                type:config.type.toUpperCase(),
                rotate:pin.rotate,
                r:config.r,
                pinType:config.pinType,
                outOffset:util.copyObject(config.outOffset),
                inOffset:util.copyObject(config.inOffset),
                drillOption:util.copyObject(config.drillOption)
            }
            var type = result.type;
            if(type === "C" || type === "OC" || type === "POLY"){
                result.r = config.r;
                if(type === "POLY"){
                    result.num = config.num;
                }
            }
            if(type === "S" || type === "CS" || type === "OV"){
                result.w = config.w;
                result.h = config.h;
            }
            return result;
        }

        // 自适应过孔大小
        function drillSize(config){
            if(!config.r){
                config.r = config.w>config.h?config.h:config.w;
            }

            if(config.type === "C"){
                config.w = config.r;
                config.h = config.r;
            }else{
                config.w = config.w || config.r;
                config.h = config.h || config.r;
            }
            
            let drillH = config.r/2,drillW = config.r/2,drillR = config.r/2;
            
            if(config.type === "OV"){
                drillH = config.h*2/3;
                drillW = config.w*2/3;
                drillR = config.r/3;
            }
            if(config.pinType == "hole"){
                return {
                    h:h,
                    w:w,
                    r:r/2
                }
            }else{
                return {
                    h:drillH,
                    w:drillW,
                    r:drillR
                }
            }
            
            
        }

        // 生成编号
        function generateNumber(config){
            console.log(config)
            // let mode=obj.mode;
            let xnum = config.rowNumber;
            let ynum = config.colNumber;
            let txt  = config.startNum.toUpperCase();
            
            let ennum=textType(txt);
            let box=[];
            for(let i=0;i<xnum;i++){
                let row=[];
                let br=false;
                if(i>0){
                    br=true;
                }
                for(let j=0;j<ynum;j++){

                    if(i==0 && j==0){
                        row.push(txt);
                    }else{
                        txt=textAdd(txt,ennum,br);
                        row.push(txt);
                        ennum=textType(txt);
                    }

                    
                    br=false;
                }
                box.push(row);
            }
            return box;

        };

        function textType(txt){
            let num=0;
            let test_en= new RegExp("^[A-Za-z]{"+num+"}\\d+$");
            for(let i=0;i<txt.length;i++){
                if(!test_en.test(txt)){
                    num++;
                    test_en=new RegExp("^[A-Za-z]{"+num+"}\\d+$");
                }
            }
            return num;
        };

        //自增
        function textAdd(txt,ennum,br){//文本，字母个数，换行
            let text_en=/^[a-zA-Z]+$/,         //字母检测
                text_num=/^[0-9]*[1-9][0-9]*$/;//数字检测
            if(ennum==0){
                return txt/1+1;
            }else{//字母
                let en=txt.substr(0,ennum),
                    num=txt.substring(ennum);

                if(text_en.test(en)&&text_num.test(num)){//字母加数字
                    if(br){
                        txt=en_add(en)+(1);
                    }else{
                        txt=en+(num/1+1);
                    }
                    
                }else if(text_en.test(en)&&!text_num.test(num)){//纯字母
                    txt=en_add(en,br);
                    
                }
                return txt;
            }

            
            function en_add(entxt,br){//纯字母增
                let length=entxt.length-1;
                let en_end=entxt.substring(length);
                let en_new="";
                let en_head=entxt.substr(0,length);
                let enAddlock=true;
                let exclude =["I","O","Q","S","X","Z"]; //需要跳过的字母

                if(en_end.toUpperCase()=="Y"||br){
                    let endnum=-1;
                    if(br){
                        length=length-1;//摒弃最后一位
                    }
                    for(let i=length;i>=0;i--){
                        en_end=entxt.substr(i,length-i+1);
                        if(en_end.toUpperCase()!="Y"){
                            endnum=i;
                            break;
                        }
                    }
                    if(endnum==-1){
                        en_end="Y"
                        endnum=0;
                    }

                    if(br){
                        if(entxt.length==1){
                            en_end=entxt;
                            endnum=0;
                            console.log(en_end)
                        }else{
                            for(let k=endnum;k<length+1;k++){
                                en_new="A"+en_new;
                            }
                        }
                        
                    }else{
                        for(let k=endnum;k<length;k++){
                            en_new="A"+en_new;
                        }
                    }
                    
                    en_head=entxt.substr(0,endnum);
                    if(en_end.toUpperCase()=="Y"){
                        en_end="A";
                        if(en_head==""||en_head==null){
                            en_head="A";
                        }
                    }else{
                        en_end=String.fromCharCode(en_end.charCodeAt(0) + 1);
                        //检测是否是需要跳过的字母
                        exclude.forEach(value=>{
                            if(value==en_end){
                                en_end = String.fromCharCode(en_end.charCodeAt(0) + 1);
                            }
                        });
                    }
                    entxt=en_head+en_end+en_new;
                    
                }else{
                    en_end=String.fromCharCode(en_end.charCodeAt(0) + 1);
                    //检测是否是需要跳过的字母
                    exclude.forEach(value=>{
                        if(value==en_end){
                            en_end = String.fromCharCode(en_end.charCodeAt(0) + 1);
                        }
                    });
                    
                    entxt=en_head+en_end;
                }
                return entxt;
            }
        };

        // 生成列阵
        function generateArrays(config){
            console.log(config);
            // colNumber: 4
            // role: "row"
            // rowNumber: 4
            // show: true
            // startNum: 1
            // startX: 1
            // startY: 2
            // type: "arrays"
            // xSpacing: -3
            // ySpacing: -3
            let pinNumberList = generateNumber(config);
            console.log(pinNumberList)
            let pinList = [];

            let nowRow = config.ySpacing < 0 ? 0 : config.rowNumber - 1;

            for(let i = 0; i < config.rowNumber; i++){

                let nowCol = config.xSpacing < 0 ? config.colNumber - 1 : 0;

                for(let j = 0; j < config.colNumber; j++){
                    let pinConfig = {
                        padid:config.padid,
                        pinid: ++config.pinid,
                        pin_num:pinNumberList[i][j],
                    }
                    let point = {
                        x:config.startX + config.xSpacing * nowCol,
                        y:config.startY + config.ySpacing * nowRow
                    }
                    pinList.push(createPin(pinConfig,point));
                    config.xSpacing < 0 ? nowCol-- :  nowCol++;
                }
                config.ySpacing < 0 ? nowRow++ : nowRow--;
            }

            return {
                pins:pinList,
                pinid:config.pinid
            }
        }

        return {
            pin,

            pad,

            pinList,

            padList,

            config,

            pinInfo,

            createNewPin:false,

            clickEvent(point){
                this.pin.x = point.x;
                this.pin.y = point.y;

                this.pinInfo.pin_num++;
                this.pinInfo.pinid++;
                this.pin = createPin(this.pinInfo,point);
                this.pinList.push(this.pin);

                return returnEditData(this.pin,this.config);
            },

            moveEvent(point){
                if(this.createNewPin){
                    this.pinInfo.pin_num++;
                    this.pinInfo.pinid++;
                    this.pin = createPin(this.pinInfo,point);
                    this.pinList.push(this.pin);
                    this.createNewPin = false;
                }

                this.pin.x = point.x;
                this.pin.y = point.y;

                return returnEditData(this.pin,this.config);
            },

            edit(editData){
                // 阵列放置
                if(editData.type == "arrays"){
                    editData.pinid = pinInfo.pinid;
                    editData.padid = pinInfo.padid;
                    let pinsObj = generateArrays(editData);
                    this.pinList.splice(-1,0,pinsObj.pins);
                    this.pinInfo.pinid = ++pinsObj.pinid;
                    this.pin.pinid = ++pinInfo.pinid;
                    return;
                }

                if(editData.type!="SELF"){
                    var baseDataChange = false,
                        typeDataChange = false;
                    if(this.config.outOffset.x == editData.outOffset.x/1
                        && this.config.outOffset.y == editData.outOffset.y/1
                        && this.config.inOffset.x == editData.inOffset.x/1
                        && this.config.inOffset.y == editData.inOffset.y/1
                        && this.config.drillOption.w == editData.drillOption.w/1
                        && this.config.drillOption.h == editData.drillOption.h/1
                        && this.config.drillOption.r == editData.drillOption.r/1
                        && this.config.pinType == editData.pinType
                        && this.config.type == editData.type){
                            baseDataChange = true;
                        }

                    this.config.pinType = editData.pinType;
                    this.config.type = editData.type;

                    this.config.outOffset.x = editData.outOffset.x/1;

                    this.config.outOffset.y = editData.outOffset.y/1;

                    this.config.inOffset.x = editData.inOffset.x/1;

                    this.config.inOffset.y = editData.inOffset.y/1;

                    this.config.drillOption.w = editData.drillOption.w/1;

                    this.config.drillOption.h = editData.drillOption.h/1;

                    this.config.drillOption.r = editData.drillOption.r/1;

                    if(editData.type === "C" || editData.type === "OC" || editData.type === "POLY"){
                        if(this.config.r == editData.r / 1){
                            typeDataChange = true;
                        }
                        this.config.r = Number(editData.r);
                        console.log(this.config.r);
                        if(editData.type === "POLY"){
                            if(this.config.num == editData.num / 1){
                                typeDataChange = true;
                            }
                            this.config.num = Number(editData.num);
                        }
                    }
                    if(editData.type === "S" || editData.type === "CS" || editData.type === "OV"){
                        if(this.config.w == editData.w / 1 && this.config.h == editData.h / 1){
                            typeDataChange = true;
                        }
                        this.config.w = Number(editData.w);
                        this.config.h = Number(editData.h);
                    }
                    if(typeDataChange && baseDataChange){
                        return;
                    }
                    this.pad = generateFigure.pad(this.config);
                    if(this.pinList.length === 1){
                        this.pad.id = this.pinInfo.padid;
                        this.padList[0] = this.pad;
                    }else{
                        let lastPin = this.pinList[this.pinList.length - 2];
                        if(lastPin instanceof Array){
                            console.log(lastPin[0].padid,this.pin.padid)
                        }
                        if((lastPin instanceof Array && lastPin[0].padid == this.pin.padid) || lastPin.padid  == this.pin.padid){
                            this.pad.id = ++this.pinInfo.padid;
                            this.padList.push(this.pad);
                            this.pin.padid = this.pad.id;
                        }else{
                            this.pad.id = this.pin.padid;
                            this.padList[padList.length - 1] = this.pad;
                            // console.log(pad,pin,padList)
                        }
                    }
                }
            },

            backout(){
                if(this.pinList.length == 1){
                    return;
                }
                // 判断上一个pin引用的是否是这个padid
                let pin = this.pinList[this.pinList.length - 2];
                if(pin instanceof Array){
                    if(this.pinList.length == 2){
                        this.pinList.shift();
                        return;
                    }
                    this.pinInfo.pinid -= pin.length;
                    this.pinList.splice(-2,1);
                    return;
                }else{
                    this.pinInfo.pinid--;
                }
                
                this.pinInfo.pin_num--;

                this.pinList.pop();


                this.pin = this.pinList[this.pinList.length - 1];

                return returnEditData(this.pin,this.config);
            },

            save(type){
                if(type!=="once"){
                    this.pinList.pop();
                    this.pinList.pop();
                }
                if(!this.createNewPin){
                    pinList.pop();
                }

                return {
                    pins:this.pinList.flat(),
                    pads:this.padList
                }
            },

            getData(){
                return handleUtil.handlerPinDraw(pinList.flat(),padList);
            },
        }
    },

    PATH:{
        LINE(options){
            return {
                path:[],

                line:{},

                createNew:false,

                config:{
                    // 线条宽度
                    width: options.units === "mm"?0.5:10,

                    // 所在层级
                    layer: options.layer || "st",

                    form:"LINE"
                },

                clickEvent(point){
                    
                    this.line = generateFigure.createLine(this.config);

                    if(this.path.length === 0 && point){
                        this.line.data.st_x = point.x;
                        this.line.data.st_y = point.y;
                        this.line.data.sp_x = point.x;
                        this.line.data.sp_y = point.y;
                    }else{
                        this.line.data.st_x = this.path[this.path.length - 1].data.sp_x;
                        this.line.data.st_y = this.path[this.path.length - 1].data.sp_y;
                        this.line.data.sp_x = this.path[this.path.length - 1].data.sp_x;
                        this.line.data.sp_y = this.path[this.path.length - 1].data.sp_y;
                    }

                    this.path.push(this.line);

                    return this.line;
                },

                moveEvent(point){
                    if(!this.path.length){
                        return {};
                    }
                    if(this.createNew){
                        this.clickEvent();
                        this.createNew = false;
                    }
                    this.line.data.sp_x = point.x;
                    this.line.data.sp_y = point.y;

                    return this.line;
                },

                backout(){
                    let line = this.path.splice(-2,1)[0];
                    if(this.path.length){
                        this.line.data.st_x = line.data.st_x;
                        this.line.data.st_y = line.data.st_y;
                    }
                    return this.line;
                },

                edit(eLine){
                    this.path.forEach((line)=>{
                        line.layer = eLine.layer;
                        line.data.width = eLine.data.width;
                    });
                    if(this.line.data.sp_x !== eLine.data.sp_x || this.line.data.sp_y !== eLine.data.sp_y){
                        this.createNew = true;
                    }
                    this.config.layer = eLine.layer;
                    this.config.width = eLine.data.width;
                    this.line.data.sp_x = eLine.data.sp_x;
                    this.line.data.sp_y = eLine.data.sp_y;
                },

                save(type){
                    if(type === "once"){
                        this.path.splice(-1,1);
                    }else{
                        this.path.splice(-3,3);
                    }
                    
                    let saveData = this.clearInvalidLine(this.path);
                    return saveData;
                },

                // 清除无效线条
                clearInvalidLine(paths){
                    return paths.filter((line)=>{
                        return line.data.st_x != line.data.sp_x || line.data.st_y != line.data.sp_y;
                    });
                },

                getData(){
                    return this.path;
                }
            }
        },

        ARC(options){
            return {
                path:[],

                arc:{},

                createNew:true,

                config:{
                    // 线条宽度
                    width: options.units === "mm"?0.5:10,

                    // 所在层级
                    layer: options.layer || "st",

                    form:"ARC",

                    arc_r:"nil"
                },

                clickEvent(point){
                    if(this.createNew){
                        this.createNew = false;
                        let arc = generateFigure.createLine(this.config);
                        arc.data.st_x = point.x;
                        arc.data.st_y = point.y;
                        arc.data.sp_x = point.x;
                        arc.data.sp_y = point.y;

                        this.arc = arc;

                        this.path.push(arc);
                        
                    }else{
                        this.createNew = true;
                    }

                    return this.arc;
                },

                moveEvent(point){
                    if(!this.path.length || this.createNew){
                        return {};
                    }
                    
                    this.arc.data.sp_x = point.x;
                    this.arc.data.sp_y = point.y;
                    this.arc.data = util.computedArc(this.arc.data);
                    return this.arc;
                },

                backout(){
                    if(this.createNew){
                        this.createNew = false;
                        return;
                    }
                    
                    this.path.pop();

                    this.arc = this.path[this.path.length - 1];

                    this.createNew = false;

                    return this.arc;
                },

                edit(eArc){
                    this.path.forEach((arc)=>{
                        arc.layer = eArc.layer;
                    });
                    if(this.arc.data.sp_x !== eArc.data.sp_x || this.arc.data.sp_y !== eArc.data.sp_y || this.arc.data.st_x !== eArc.data.st_x || this.arc.data.st_y !== eArc.data.st_y){
                        this.createNew = true;
                        this.arc.data.sp_x = eArc.data.sp_x;
                        this.arc.data.sp_y = eArc.data.sp_y;
                        this.arc.data.st_x = eArc.data.st_x;
                        this.arc.data.st_y = eArc.data.st_y;
                        this.arc.data = util.computedArc(this.arc.data);
                    }
                    this.arc.data.width = eArc.data.width;
                    this.arc.data.arc_r = eArc.data.arc_r;
                    this.config.layer = eArc.layer;
                    this.config.width = eArc.data.width;
                    this.config.arc_r = eArc.data.arc_r;
                },

                save(){
                    if(!this.createNew){
                        this.path.splice(-2,2);
                    }
                    
                    let saveData = this.clearInvalidLine(this.path);
                    return saveData;
                },

                // 清除无效线条
                clearInvalidLine(paths){
                    return paths.filter((line)=>{
                        return line.data.st_x != line.data.sp_x || line.data.st_y != line.data.sp_y;
                    });
                },

                getData(){
                    return this.path;
                }
            
            }
        },

        PATH(options){
            return {
                path:[],

                line:{},

                createNew:false,

                config:{
                    // 线条宽度
                    width: options.units === "mm"?0.5:10,

                    // 所在层级
                    layer: options.layer || "st",

                    form:"LINE",
                    
                    arc_r:"nil"
                },

                clickEvent(point){
                    
                    this.line = generateFigure.createLine(this.config);

                    if(this.path.length === 0 && point){
                        this.line.data.st_x = point.x;
                        this.line.data.st_y = point.y;
                        this.line.data.sp_x = point.x;
                        this.line.data.sp_y = point.y;
                    }else{
                        this.line.data.st_x = this.path[this.path.length - 1].data.sp_x;
                        this.line.data.st_y = this.path[this.path.length - 1].data.sp_y;
                        this.line.data.sp_x = this.path[this.path.length - 1].data.sp_x;
                        this.line.data.sp_y = this.path[this.path.length - 1].data.sp_y;
                    }

                    this.path.push(this.line);

                    return this.line;
                },

                moveEvent(point){
                    if(!this.path.length){
                        return {};
                    }
                    if(this.createNew){
                        this.clickEvent();
                        this.createNew = false;
                    }
                    this.line.data.sp_x = point.x;
                    this.line.data.sp_y = point.y;

                    if(this.line.form == "ARC"){
                        this.line.data = util.computedArc(this.line.data);
                    }

                    return this.line;
                },

                backout(){
                    let line = this.path.splice(-2,1)[0];
                    if(this.path.length){
                        this.line.data.st_x = line.data.st_x;
                        this.line.data.st_y = line.data.st_y;
                    }
                    return this.line;
                },

                edit(eLine){
                    this.path.forEach((line)=>{
                        line.layer = eLine.layer;
                        line.data.width = eLine.data.width;
                    });

                    if(this.line.data.sp_x !== eLine.data.sp_x || this.line.data.sp_y !== eLine.data.sp_y){
                        this.createNew = true;
                        this.line.data.sp_x = eLine.data.sp_x;
                        this.line.data.sp_y = eLine.data.sp_y;
                    }
                    this.config.layer = eLine.layer;
                    this.config.width = eLine.data.width;
                    this.config.form = eLine.form;
                    
                    this.line.form = eLine.form;
                    this.line.data.name = eLine.form;

                    if(this.line.form == "ARC"){
                        this.line.data = util.computedArc(this.line.data);
                        this.line.data.arc_r = eLine.data.arc_r;
                    }
                },

                save(type){
                    if(type === "once"){
                        this.path.splice(-1,1);
                    }else{
                        this.path.splice(-3,3);
                    }
                    let saveData = this.clearInvalidLine(this.path);
                    return saveData;
                },

                // 清除无效线条
                clearInvalidLine(paths){
                    return paths.filter((line)=>{
                        return line.data.st_x != line.data.sp_x || line.data.st_y != line.data.sp_y;
                    });
                },

                getData(){
                    return this.path;
                }
            }
        },

        RECT(options){
            options.type = "S";
            return this._AkinRect(options);
        },

        OVAL(options){
            options.type = "OV";
            return this._AkinRect(options);
        },

        CIRCLE(options){
            return {
                path:[],

                arc:{},

                status:0,

                config:{
                    // 线条宽度
                    width:options.units === "mm"?0.5:10,

                    // 所在层级
                    layer: options.layer || "st",

                    form:"ARC",

                    arc_r:"nil"
                },

                clickEvent(point){

                    if(this.status == 0){
                        this.status++;

                        let arc = generateFigure.createLine(this.config);
                        arc.data.o_x = point.x;
                        arc.data.o_y = point.y;

                        this.arc = arc;

                        this.path.push(arc);
                        
                    }else{
                        if(this.status == 3){
                            this.status = 0;
                            return;
                        }else if(this.status == 2){

                            let computedPoint = util.computedLineInCircle(point,{x:this.arc.data.o_x,y:this.arc.data.o_y},this.arc.data.r);
                            this.arc.data.st_x = computedPoint.x;
                            this.arc.data.st_y = computedPoint.y;
                            this.arc.data.sp_x = computedPoint.x;
                            this.arc.data.sp_y = computedPoint.y;

                            // this.drawObj.drawTop.clear();
                        }
                        this.status++;
                    }

                    return this.arc;
                },

                moveEvent(point){
                    if(!this.path.length || this.status == 0){
                        return {};
                    }
                    if(this.status == 1){
                        this.arc.data.r = Math.sqrt((this.arc.data.o_x - point.x) ** 2 + (this.arc.data.o_y - point.y) ** 2);
                    }
                    else if(this.status == 2){
                        let computedPoint = util.computedLineInCircle(point,{x:this.arc.data.o_x,y:this.arc.data.o_y},this.arc.data.r);
                        this.arc.data.st_x = computedPoint.x;
                        this.arc.data.st_y = computedPoint.y;
                        this.arc.data.sp_x = computedPoint.x;
                        this.arc.data.sp_y = computedPoint.y;
                    }else if(this.status == 3){
                        let computedPoint = util.computedLineInCircle(point,{x:this.arc.data.o_x,y:this.arc.data.o_y},this.arc.data.r);
                        this.arc.data.sp_x = computedPoint.x;
                        this.arc.data.sp_y = computedPoint.y;
                    }
                    
                    return this.arc;
                },

                backout(){
                    if(this.status == 1){
                        this.status = 0;
                        this.path.pop();
                        this.arc = this.path[this.path.length - 1];
                        return;
                    }
                    if(this.status == 2){
                        this.status = 1;
                        this.arc.data.st_x = 0;
                        this.arc.data.st_y = 0;
                        return;
                    }

                    if(this.status == 3){
                        this.status = 2;
                        this.arc.data.sp_x = this.arc.data.st_x;
                        this.arc.data.sp_y = this.arc.data.st_y;
                        return;
                    }

                    if(this.status == 0){
                        this.status = 3;
                        return;
                    }

                    return this.arc;
                },

                edit(eArc){
                    this.path.forEach((arc)=>{
                        arc.layer = eArc.layer;
                    });
                    if(this.arc.data.r != eArc.data.r && this.status == 1){
                        this.status = 2;
                        this.arc.data.r = eArc.data.r;
                    }

                    this.arc.data.width = eArc.data.width;
                    this.arc.data.arc_r = eArc.data.arc_r;
                    this.arc.data.o_x = eArc.data.o_x;
                    this.arc.data.o_y = eArc.data.o_y;

                    this.config.layer = eArc.layer;
                    this.config.width = eArc.data.width;
                    this.config.arc_r = eArc.data.arc_r;
                },

                save(type){
                    if(type == "once"){
                        if(this.status != 0){
                            this.path.splice(-1,1);
                        }
                    }else{
                        if(this.status < 2){
                            this.path.splice(-2,2);
                        }else{
                            this.path.splice(-1,1);
                        }
                    }
                    
                    let saveData = this.clearInvalidLine(this.path);
                    return saveData;
                },

                // 清除无效线条
                clearInvalidLine(paths){
                    return paths.filter((line)=>{
                        return !!line.data.r;
                    });
                },

                getData(){
                    return this.path;
                }
            }
        },
    },


    pathShape(options){
        return {
            shapes:[],

            shape:{},

            lineData:{},

            startLineData:{},

            endLineData:{},

            createNew:false,

            createNewShape:true,

            config:{
                // 所在层级
                layer: options.layer || "st",

                form:"LINE",
                
                arc_r:"t"
            },

            clickEvent(point){

                if(this.createNewShape){
                    this.shape = {
                        form:"PATH",
                        layer:this.config.layer,
                        type:"shapes",
                        make:"o",
                        data:[]
                    };
                    this.shapes.push(this.shape);   
                    this.createNewShape = false;
                }
                if(this.shape.data.length === 0){
                    this.config.form = "LINE";
                    this.lineData = generateFigure.createLineData(this.config);
                    this.endLineData = generateFigure.createLineData(this.config);
                    this.lineData.st_x = point.x;
                    this.lineData.st_y = point.y;
                    this.lineData.sp_x = point.x;
                    this.lineData.sp_y = point.y;
                    this.startLineData = this.lineData;
                    this.endLineData.sp_x = this.startLineData.st_x;
                    this.endLineData.sp_y = this.startLineData.st_y;
                    this.shape.data.push(this.lineData);
                    this.shape.data.push(this.endLineData);

                }else{
                    if(this.lineData.sp_x == this.startLineData.st_x && this.lineData.sp_y == this.startLineData.st_y){
                        this.shape.data.pop();
                        this.createNewShape = true;
                        return {};
                    }
                    if(this.lineData.st_x == this.lineData.sp_x && this.lineData.st_y == this.lineData.sp_y){
                        this.createNewShape = true;
                        return {};
                    }
                    this.lineData = generateFigure.createLineData(this.config);
                    this.lineData.st_x = this.shape.data[this.shape.data.length - 2].sp_x;
                    this.lineData.st_y = this.shape.data[this.shape.data.length - 2].sp_y;
                    this.lineData.sp_x = this.shape.data[this.shape.data.length - 2].sp_x;
                    this.lineData.sp_y = this.shape.data[this.shape.data.length - 2].sp_y;
                    this.shape.data.splice(-1,0,this.lineData);
                }

                return {...this.lineData,layer:this.config.layer,make:this.shape.make};
            },

            moveEvent(point){
                if(!this.shape || !this.shape.data || !this.shape.data.length || this.createNewShape){
                    return {};
                }
                if(this.createNew){
                    this.lineData = generateFigure.createLineData(this.config);
                    this.lineData.st_x = this.shape.data[this.shape.data.length - 1].sp_x;
                    this.lineData.st_y = this.shape.data[this.shape.data.length - 1].sp_y;
                    this.lineData.sp_x = this.shape.data[this.shape.data.length - 1].sp_x;
                    this.lineData.sp_y = this.shape.data[this.shape.data.length - 1].sp_y;
                    this.shape.data.splice(-1,0,this.lineData);
                    this.createNew = false;
                }
                this.lineData.sp_x = point.x;
                this.lineData.sp_y = point.y;
                if(this.lineData.name == "ARC"){
                    let h = util.computedArc(this.lineData);
                    this.lineData.r = h.r;
                    this.lineData.o_x = h.o_x;
                    this.lineData.o_y = h.o_y;
                }
                this.endLineData.st_x = point.x;
                this.endLineData.st_y = point.y;

                let result = {
                    ...util.copyObject(this.lineData),
                    layer:this.config.layer,
                    make:this.shape.make
                }

                if(this.startLineData === this.lineData){
                    result.first = true;
                }

                return result;
            },

            backout(){
                if(this.createNewShape){
                    this.createNewShape = false;
                    this.endLineData = this.shape.data[this.shape.data.length - 1];
                    this.lineData = this.shape.data[this.shape.data.length - 2];
                }else{
                    let lastLine = this.shape.data.splice(-2,1)[0];
                    this.endLineData.st_x = lastLine.st_x;
                    this.endLineData.st_y = lastLine.st_y;
                    this.lineData = this.shape.data[this.shape.data.length - 2];
                    if(this.shape.data.length === 1){
                        this.shapes.pop();
                        this.shape = this.shapes[this.shapes.length - 1];
                        this.createNewShape = true;
                    }
                }
            },

            edit(eLine){
                this.shapes.forEach((shape)=>{
                    shape.layer = eLine.layer;
                });
                if(this.lineData.sp_x !== eLine.sp_x || this.lineData.sp_y !== eLine.sp_y){
                    this.createNew = true;
                }
                this.config.layer = eLine.layer;
                this.config.arc_r = eLine.arc_r;
                this.config.form = eLine.name;
                this.lineData.sp_x = eLine.sp_x;
                this.lineData.sp_y = eLine.sp_y;
                this.endLineData.st_x = this.lineData.sp_x;
                this.endLineData.st_y = this.lineData.sp_y;

                this.lineData.name = eLine.name;
                this.lineData.arc_r = eLine.arc_r;

                this.shape.make = eLine.make;
                if(eLine.name === "ARC"){
                    let h = util.computedArc(this.lineData);
                    this.lineData.r = h.r;
                    this.lineData.o_x = h.o_x;
                    this.lineData.o_y = h.o_y;
                }
            },

            save(type){
                if(type === "once"){
                    if(!this.createNewShape){
                        let lastLine = this.shape.data.splice(-2,1)[0];
                        this.endLineData.st_x = lastLine.st_x;
                        this.endLineData.st_y = lastLine.st_y;
                    }
                }

                return this.clearInvalidShape(this.shapes);
            },

            // 过滤无效铜皮
            clearInvalidShape(shapes){
                return shapes.filter((shape)=>{
                    shape.data = shape.data.filter(line=>{
                        return line.st_x != line.sp_x || line.st_y != line.sp_y;
                    });
                    if(shape.data.length < 2){
                        return false;
                    }
                    if(shape.data.length == 2){
                        if(shape.data[1].name == "ARC"){
                            return true;
                        }
                        return false;
                    }
                    return true;
                });
            },

            getData(){
                return this.shapes;
            }
        }
    },

    circleShape(options){
        return {
            shapes:[],

            shape:{},

            circleData:{},

            createNewShape:true,

            config:{
                // 所在层级
                layer: options.layer || "st",

                form:"ARC",

                make:"o"
            },

            clickEvent(point){

                if(this.createNewShape){
                    this.shape = {
                        form:"PATH",
                        layer:this.config.layer,
                        type:"shapes",
                        make:this.config.make,
                        data:[]
                    };
                    
                    this.circleData = generateFigure.createLineData(this.config);

                    this.circleData.o_x = point.x;
                    this.circleData.o_y = point.y;

                    this.shape.data.push(this.circleData);

                    this.shapes.push(this.shape);  

                    this.createNewShape = false;
                }else{
                    this.circleData.r = Math.sqrt(Math.pow(this.circleData.o_x - point.x,2) + Math.pow(this.circleData.o_y - point.y,2));
                    this.createNewShape = true;
                }

                return {...this.circleData,layer:this.config.layer,make:this.shape.make,r: this.circleData.r * 2};
            },

            moveEvent(point){
                if(this.createNewShape){
                    return {...this.circleData,layer:this.config.layer,make:this.shape.make,r: this.circleData.r * 2};
                }

                this.circleData.r = Math.sqrt(Math.pow(this.circleData.o_x - point.x,2) + Math.pow(this.circleData.o_y - point.y,2));

                let result = {
                    ...this.circleData,
                    layer:this.config.layer,
                    make:this.shape.make,
                    r:this.circleData.r * 2
                }

                return result;
            },

            backout(){
                if(this.createNewShape){
                    this.createNewShape = false;
                }else{
                    this.shapes.pop();

                    this.shape = this.shapes[this.shapes.length - 1];

                    this.circleData = this.shape.data[0];

                    this.createNewShape = true;
                }
            },

            edit(eCircle){
                this.shapes.forEach((shape)=>{
                    shape.layer = eCircle.layer;
                });

                if(this.circleData.r !== eCircle.r / 2){
                    this.createNewShape = true;
                    this.circleData.r = eCircle.r / 2
                }
                this.circleData.o_x = eCircle.o_x;
                this.circleData.o_y = eCircle.o_y;

                this.shape.make = eCircle.make;

            },

            save(type){
                if(!this.createNewShape){
                      this.shapes.pop();  
                }

                return this.clearInvalidShape(this.shapes);
            },

            // 过滤无效铜皮
            clearInvalidShape(shapes){
                return shapes.filter((shape)=>{
                    return shape.data[0].r != 0;
                });
            },

            getData(){
                return this.shapes;
            }
        }
    },

    rectShape(options){

        function rectData2LineData(rectData){
            return toOriginMathods.rect2Path(rectData);
        }

        return {
            shapes:[],

            shape:{},

            rectData:{},

            createNewShape:true,

            config:{
                // 所在层级
                layer: options.layer || "st",

                make:"o"
            },

            clickEvent(point){
                if(this.createNewShape){
                    this.shape = {
                        form:"PATH",
                        layer:this.config.layer,
                        type:"shapes",
                        make:this.config.make,
                        data:[]
                    };
                    
                    this.rectData = {
                        x: point.x,
                        y: point.y,
                        w:0,
                        h:0
                    };

                    this.shapes.push(this.shape);  

                    this.createNewShape = false;
                }else{
                    this.shape.data = rectData2LineData(this.rectData);
                    this.createNewShape = true;
                }

                return {...this.rectData,layer:this.config.layer,make:this.shape.make};
            },

            moveEvent(point){
                if(this.createNewShape){
                    return {...this.circleData,layer:this.config.layer,make:this.shape.make};
                }

                this.rectData.w = Math.abs(this.rectData.x - point.x) * 2;

                this.rectData.h = Math.abs(this.rectData.y - point.y) * 2;
                
                this.shape.data = rectData2LineData(this.rectData);

                return {...this.rectData,layer:this.config.layer,make:this.shape.make};
            },

            backout(){
                if(this.createNewShape){
                    this.createNewShape = false;
                }else{
                    this.shapes.pop();

                    this.shape = this.shapes[this.shapes.length - 1];

                    // this.circleData = this.shape.data[0];

                    this.createNewShape = true;
                }
            },

            edit(eRect){
                this.shapes.forEach((shape)=>{
                    shape.layer = eRect.layer;
                });

                if(this.rectData.w !== eRect.w || this.rectData.h !== eRect.h){
                    this.createNewShape = true;
                    this.rectData.w = eRect.w;
                    this.rectData.h = eRect.h;
                }

                this.rectData.x = eRect.x;
                this.rectData.y = eRect.y;

                this.shape.data = rectData2LineData(this.rectData);
                this.shape.make = eRect.make;

            },

            save(type){
                if(!this.createNewShape){
                      this.shapes.pop();  
                }

                return this.clearInvalidShape(this.shapes);
            },

            // 过滤无效铜皮
            clearInvalidShape(shapes){
                return shapes.filter((shape)=>{
                    shape.data = shape.data.filter(line=>{
                        return line.st_x != line.sp_x || line.st_y != line.sp_y;
                    });
                    if(shape.data.length < 2){
                        return false;
                    }
                    return true;
                });
            },

            getData(){
                return this.shapes;
            }
        }
    },

    text(options){
        let config = {
            layer: options.layer || "st",
            content:"",
            textblock:2
        };

        let initalText = generateFigure.createText(config);
        
        return {
            texts:[initalText],

            text:initalText,

            config,

            createNew:false,

            clickEvent(point){

                    this.text = generateFigure.createText(this.config);

                    this.text.data.x = point.x;
                    this.text.data.y = point.y;

                    this.texts.push(this.text);

                    return this.text;
            },

            moveEvent(point){
                if(this.createNew){
                    this.text = generateFigure.createText(this.config);
                    this.texts.push(this.text);
                    this.createNew = false;
                }
                this.text.data.x = point.x;
                this.text.data.y = point.y;

                return this.text;
            },

            backout(){
                if(this.texts.length == 1){
                    return;
                }
                this.texts.pop();
                this.text = this.texts[this.texts.length - 1];
            },

            edit(eText){
                this.config.layer = eText.layer;
                if(this.texts.length === 0){
                    return;
                }
                if(eText.data.x != this.text.data.x || eText.data.y != this.text.data.y){
                    this.createNew = true;
                    this.text.data.x = eText.data.x;
                    this.text.data.y = eText.data.y;
                }
                this.texts.forEach(text=>{
                    text.layer = eText.layer;
                });
                this.text.data.rotate = eText.data.rotate;
                this.text.data.content = String(eText.data.content);
                this.text.data.textblock = eText.data.textblock;
                this.text.data.textheight = this.text.data.textblock;
                this.text.data.textwidth = this.text.data.textblock * this.text.data.content.length;

                this.config.textblock = eText.data.textblock;
            },

            save(type){
                if(type!=="once"){
                    this.texts.pop();
                    this.texts.pop();
                }
                if(!this.createNew){
                    this.texts.pop();
                }

                return this.texts;
            },

            getData(){
                return this.texts;
            }
        }
    },

    // 一脚标识
    onePinMark(options){
        var config = {
            r: options.units == "mm" ? 0.8 : 31.496,
            x: 0,
            y: 0,
            rotate: 0
        }

        var initalOnePinMark = generateFigure.createOnePinTMark(config);

        function drawData(onePinMarkList){
            let draw = onePinMarkList.flat();
            return draw;
        }

        return {
            onePinMarkList:[initalOnePinMark],

            onePinMark:initalOnePinMark,

            config,

            createNew:false,

            clickEvent(point){
                    this.config.x = point.x;
                    this.config.y = point.y;

                    this.onePinMark = generateFigure.createOnePinTMark(this.config);

                    this.onePinMarkList.push(this.onePinMark);

                    return this.config;
            },

            moveEvent(point){
                this.config.x = point.x;
                this.config.y = point.y;

                this.onePinMark = generateFigure.createOnePinTMark(this.config);

                if(this.createNew){
                    this.onePinMarkList.push(this.onePinMark);
                    this.createNew = false;
                }else{
                    this.onePinMarkList[this.onePinMarkList.length - 1] = this.onePinMark;
                }
                return this.config;
            },

            backout(){
                if(this.onePinMarkList.length == 1){
                    return;
                }
                this.onePinMarkList.pop();
                this.onePinMark = this.onePinMarkList[this.onePinMarkList.length - 1];
            },

            edit(eOnePinMark){
                if(this.onePinMarkList.length === 0){
                    return;
                }
                this.config.rotate = eOnePinMark.rotate;
                if(eOnePinMark.x != this.config.x || eOnePinMark.y != this.config.y){
                    this.createNew = true;
                    this.config.x = eOnePinMark.x;
                    this.config.y = eOnePinMark.y;
                }
                this.config.r = eOnePinMark.r;
                this.onePinMark = generateFigure.createOnePinTMark(this.config);
                this.onePinMarkList[this.onePinMarkList.length - 1] = this.onePinMark;
            },

            save(type){
                if(type!=="once"){
                    this.onePinMarkList.pop();
                    this.onePinMarkList.pop();
                }
                if(!this.createNew){
                    this.onePinMarkList.pop();
                }

                return drawData(this.onePinMarkList);
            },

            getData(){
                return drawData(this.onePinMarkList);
            }
        }
    },

    // 一脚圆标识
    onePinCMark(options){
        var config = {
            r: options.units == "mm" ? 0.8 : 31.496
        }

        var initalCMark = generateFigure.createOnePinCMark(config);

        function editData(config,onePinMark){
            var cMark = util.copyObject(onePinMark);

            var edit = {
                r: config.r,
                o_x: onePinMark.data.o_x,
                o_y: onePinMark.data.o_y
            }

            cMark.data.r *= 2;

            return edit;
        }

        return {
            onePinMarkList:[initalCMark],

            onePinMark:initalCMark,

            config,

            createNew:false,

            clickEvent(point){

                    this.onePinMark = generateFigure.createOnePinCMark(this.config);

                    this.onePinMark.data.o_x = point.x;
                    this.onePinMark.data.o_y = point.y;

                    this.onePinMarkList.push(this.onePinMark);

                    return editData(this.config, this.onePinMark);
            },

            moveEvent(point){

                if(this.createNew){
                    this.onePinMark = generateFigure.createOnePinCMark(this.config);
                    this.onePinMarkList.push(this.onePinMark);
                    this.createNew = false;
                }
                this.onePinMark.data.o_x = point.x;
                this.onePinMark.data.o_y = point.y;

                return editData(this.config, this.onePinMark);
            },

            backout(){
                if(this.onePinMarkList.length == 1){
                    return;
                }
                this.onePinMarkList.pop();
                this.onePinMark = this.onePinMarkList[this.onePinMarkList.length - 1];
            },

            edit(eOnePinMark){
                if(this.onePinMarkList.length === 0){
                    return;
                }
                if(eOnePinMark.o_x != this.onePinMark.data.o_x || eOnePinMark.o_y != this.onePinMark.data.o_y){
                    this.createNew = true;
                    this.onePinMark.data.o_x = eOnePinMark.o_x;
                    this.onePinMark.data.o_y = eOnePinMark.o_y;
                }
                if(eOnePinMark.r != this.config.r){
                    this.config.r = eOnePinMark.r;
                    var newOnePinMark = generateFigure.createOnePinCMark(this.config);
                    newOnePinMark.data.o_x = this.onePinMark.data.o_x;
                    newOnePinMark.data.o_y = this.onePinMark.data.o_y;
                    this.onePinMark = newOnePinMark;
                    this.onePinMarkList[this.onePinMarkList.length - 1] = this.onePinMark;
                }
            },

            save(type){
                if(type!=="once"){
                    this.onePinMarkList.pop();
                    this.onePinMarkList.pop();
                }
                if(!this.createNew){
                    this.onePinMarkList.pop();
                }

                return this.onePinMarkList;
            },

            getData(){
                return this.onePinMarkList;
            }
        }
    },

    // 复制图形
    copyBoxChooseImage(options){
        var originImage = options.copyImages;
        var center = {
            x: (options.range.x[0] + options.range.x[1]) / 2,
            y: (options.range.y[0] + options.range.y[1]) / 2
        }
        console.log(options.pinLength,options.pins_id);
        var imageConfig = {
            pins_id: options.pins_id,
            pinLength : options.pinLength
        }

        var copyImage = getNewCopyImage(originImage);

        var copyImageList = [copyImage];

        var config = {
            x: 0,
            y: 0
        }

        function getNewCopyImage(imgList){
            imgList = util.copyObject(imgList);
            var updatePinId = {};
            imgList.forEach(img=>{
                if(img.type == "pins"){
                    if(!updatePinId[img.pinid]){
                        updatePinId[img.pinid] = ++imageConfig.pins_id;
                    }
                    img.pinid = updatePinId[img.pinid];
                }
            });
            return imgList;
        }

        function getCopyImage(){
            return getNewCopyImage(originImage);
        }

        function moveImages(imgList,offset){
            imgList = util.copyObject(imgList);
            imgList.forEach(img=>{
                if(img.type == "pins" || img.form == "S" ||  img.form == "OV" ||  img.form == "OC" || img.type == "texts"){
                    img.data.x += offset.x;
                    img.data.y += offset.y;
                    return;
                }
                if(img.type == "path"){
                    img.data.st_x += offset.x;
                    img.data.st_y += offset.y;
                    img.data.sp_x += offset.x;
                    img.data.sp_y += offset.y;
                    if(img.form == "ARC"){
                        img.data.o_x += offset.x;
                        img.data.o_y += offset.y;
                    }
                    return;
                }
                if(img.type == "shapes"){
                    img.data.forEach(line=>{
                        line.st_x += offset.x;
                        line.st_y += offset.y;
                        line.sp_x += offset.x;
                        line.sp_y += offset.y;
                        if(line.name == "ARC"){
                            line.o_x += offset.x;
                            line.o_y += offset.y;
                        }
                    });
                }
            });
            return imgList;
        }

        return {
            createNew:false,

            clickEvent(point){
                var offset = {
                    x: point.x - center.x,
                    y: point.y - center.y 
                }

                copyImageList[copyImageList.length - 1] = moveImages(copyImage,offset);

                copyImage = getCopyImage();

                copyImageList.push(copyImage);

                copyImageList[copyImageList.length - 1] = moveImages(copyImage,offset);


                config.x = point.x;
                config.y = point.y;

                return config;
            },

            moveEvent(point){
                var offset = {
                    x: point.x - center.x,
                    y: point.y - center.y 
                }

                config.x = point.x;
                config.y = point.y;

                if(this.createNew){
                    copyImage = getCopyImage();
                    copyImageList.push(copyImage);
                    this.createNew = false;
                }
                copyImageList[copyImageList.length - 1] = moveImages(copyImage,offset);
                return config;
            },

            backout(){
                if(copyImageList.length == 1){
                    return;
                }
                imageConfig.pins_id -= 2 * imageConfig.pinLength;
                copyImageList.pop();
                copyImage = getCopyImage();
            },

            edit(eOnePinMark){
                if(this.onePinMarkList.length === 0){
                    return;
                }
                if(eOnePinMark.o_x != this.onePinMark.data.o_x || eOnePinMark.o_y != this.onePinMark.data.o_y){
                    this.createNew = true;
                    this.onePinMark.data.o_x = eOnePinMark.o_x;
                    this.onePinMark.data.o_y = eOnePinMark.o_y;
                }
            },

            save(type){
                if(type!=="once"){
                    copyImageList.pop();
                    copyImageList.pop();
                }
                if(!this.createNew){
                    copyImageList.pop();
                }

                return copyImageList.flat();
            },

            getData(){
                return copyImageList.flat();
            }
        }
    }
}

